<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<%@ taglib prefix="s" uri="/struts-tags" %> 
<head>
	 <jsp:include page="/views/include.jsp"></jsp:include>
	 <script type="text/javascript">
		$.parser.onComplete = function(){
            $('body').css('visibility','visible');
            setTimeout(function(){
	        	$('#loading-mask').remove();
            },50);
        };
        
        function ajaxLoading(){ 
            $("<div class='datagrid-mask'></div>").css({display:"block",width:"100%",height:$(window).height()}).appendTo("body"); 
            $("<div class='datagrid-mask-msg'></div>").html("正在处理，请稍候。。。").appendTo("body").css({display:"block",left:($(document.body).outerWidth(true) - 190) / 2,top:($(window).height() - 45) / 2});
         } 
        
         function ajaxLoadEnd(){ 
             $(".datagrid-mask").remove(); 
             $(".datagrid-mask-msg").remove();             
        }
        
        
		$(function() {
			$(window).resize(function(){
            	$('#mainlayout').layout('resize');
            });
			
			$('#dlg').dialog({
				onOpen:function(){
					$('#dt_excel').datagrid('resize');
				}
			});
			$('#dt_excel').datagrid({onDblClickRow:function dblClickRow(rowIndex,rowData){
				$('#update-excel-form')[0].reset();
				$('#update-excel-form').formid('loadit',rowData);
				$('#update-excel-dialog').dialog('setTitle','修改表信息').dialog('open');
			}});
		});
	
		function refresh(){
			$('#dt_excel').datagrid('loadData', {total:0,rows:[]});
			$('#dt_excel').datagrid('load',{});
		}
		
		function newItem() {
			url = '<c:url value="/etl/excel_upload.tg"/>';
			$('#myform').form('clear');
			$.each($('#myform input'),function(i){
				$(this).removeAttr("readonly");
			});
			
			$("#dlg-buttons a:first-child").show();
			$('#dlg').dialog('setTitle','上传Excel文件').dialog('open');
		}
		
		function saveItem(){
			$('#excelUploadForm').form('submit',{
				url:"excel_upload.tg",
				onSubmit:function(){
					return $(this).form('validate');
				},
				success:function(result){
					$('#dlg').dialog('close');
					$('#dt_excel').datagrid('reload');
					data=eval('('+result+')');
					if(data.success){
						$("#excleName").val('');
						$("#excleMemo").val('');
						$.messager.show(
							{
								title:'提示',
								msg:data.message,
								showType:'slide'
							}
						);
					}
					if(data.error){
						var msg = data.msg.excel;
						$.messager.alert('警告','操作失败:<br>' + msg,'error');
					}
					if(data.message != null && !data.success){
						$.messager.alert('警告','操作失败:<br>' + data.message,'error');
					}
					$("#excleName").val('');
					$("#excleMemo").val('');
				}
			});
		}	
		
		function updateItem() {
			$('#update-excel-form').form('submit', {
				url:'<c:url value="/etl/excel_update.tg"/>',
				onSubmit:function() { return true; },
				dataType:"json",
				success:function(result) {
					var data = eval("(" + result + ")");
					if(data.success) {
						$.messager.show({
							title		:	'提示ʾ',
							msg			:	'操作成功!',
							showType	:	'slide'
						});
						$('#update-excel-dialog').dialog('close');
						$('#dt_excel').datagrid('loadData', {total:0,rows:[]});
						$('#dt_excel').datagrid("reload");
					} else {
						$.messager.alert('操作失败!','error');
					}
				}
			});
		}
		
		function statusFormatter(value){
			 switch (value) {
			case 0:
				return "已上传";
				break;
			case 1:
				return "处理中";
				break;
			case 2:
				return "已完成";
				break;
			default:
				return "未知";
				break;
			}
		}
		 
		function showErrors(value) {
				if (value) {
					parent.addTab("错误查看" , "<c:url value='/etl/error_index.tg' />?excelId=" + value , "");
				} else {
					$.messager.show({
						title:'注意',
						msg:'请先选择数据，再进行修改。'
					});
				}
		}
		
		function errorsFormatter(value, row , index) {
			if(value){
				return "<a href='javascript:showErrors(" + row.excel_id + ")' class='easyui-linkbutton' iconCls='icon-edit' plain='true'>查看</a> | <a href='excel_download.tg?excelId=" + row.excel_id + "' class='easyui-linkbutton' iconCls='icon-edit' plain='true'>下载</a>";
			} else {
				return "";
			}
		}
		
		function excute() {
			var row = $('#dt_excel').datagrid('getSelected');
			if (row) {
				if(row["status"] == 2){
					if(!window.confirm("该Excel文件已经执行完成,确定还要重新执行吗?")){
						return;
					}
				}
				var url = '<c:url value="/etl/mapping_excute.tg"/>';
				var excelId = row["excel_id"];
				$.ajax({
					url : url,
					type : "post",
					dataType : "json",
					data : {excelId : excelId},
					beforeSend:ajaxLoading,
					success : function (result) {
						ajaxLoadEnd(); 
						data=eval(result);
						if(data.msg != 'Excel文件不存在,请重新上传!'){
							$('#dt_excel').datagrid('loadData', {total:0,rows:[]});
							$('#dt_excel').datagrid("reload");
						}
						alert(data.msg);
					}
				});
			} else {
				$.messager.show({
					title:'注意',
					msg:'请先选择数据，再执行导入。'
				});
			}
		}
	</script>
</head>
	<body style="margin:0;padding:0;height:100%;overflow:hidden;background:#F2FBFF">
  			<div id="mainlayout" class="easyui-layout" fit="true">
			<div region="north" border="false">
				<div class="toolbar">
					<table cellpadding="0" cellspacing="0" style="width:95%;height=30px;">
						<tr>
							<td>
								<a href="javascript:excute()" class="easyui-linkbutton" iconCls="icon-edit" plain="true">执行</a>
								<a href="javascript:newItem()" class="easyui-linkbutton" iconCls="icon-add" plain="true">上传</a>
								<a href="javascript:refresh()" class="easyui-linkbutton" iconCls="icon-reload" plain="true">刷新</a>
							</td>
						</tr>
					</table>
				</div>
			</div>
			
		<!-- 上传Excel Dialog  -->
		<div region="center" border="false">
			<table id="dt_excel" class="easyui-datagrid" url="<c:url value='excel_data.tg'/>" fit="true" border="false" pagination="true" striped="true" fitColumns="true" singleSelect="true" rownumbers="true">
				<thead>
					<th field="shop_id" width="35"  >分公司ID</th>
					<th field="shopName" width="60"  >分公司名称</th>
					<th field="excel_name" width="100"  >Excel名称</th>
					<th field="excel_rowIndex" width="30"  >起始位置</th>
					<th field="status" width="40"   formatter="statusFormatter">状态</th>
					<th field="excel_uploadTime" width="100"   >上传时间</th>
					<th field="result" width="200" >处理结果</th>
					<th field="errors" width="100" formatter="errorsFormatter">错误处理</th>
				</thead>
			</table>
		</div>
			
		<!-- 上传Excel Dialog  Start-->
		<div id="dlg" class="easyui-dialog" closed="true" modal="true" buttons="#dlg-buttons">
			<form id="excelUploadForm" method="post" enctype="multipart/form-data" action="./excel_upload.tg">
				<div style="padding:20px">
					<table cellpadding="0" cellspacing="0" class="form-table">
						<tr>
							<td>请选择Excel文件：</td>
							<td><input id="excleName" name="excel" class="easyui-validatebox" type="file" required="true"></input>
						</tr>
						</tr>
						<tr>
							<td>起始行数：</td>
							<td><input id="rowIndex" name="rowIndex" class="easyui-validatebox" type="text" required="true" value='${rowIndex}'></input>
						</tr>
					</table>
				</div>
			</form>
			<div id="dlg-buttons" style="text-align:center">
				<a  href="#" class="easyui-linkbutton"  iconCls="icon-save"   onclick="javascript:saveItem()">保存</a>
				<a  href="#" class="easyui-linkbutton"  iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')">关闭</a>
			</div>
		</div>
		<!-- 上传Excel Dialog End-->
		
		
		<!-- 修改 Excel Dialog  Start-->
		<div id="update-excel-dialog" class="easyui-dialog" closed="true" modal="true" buttons="#dlg-buttons">
			<form id="update-excel-form" method="post"  action="./excel_update.tg">
				<input id="excel_id" type="hidden" name="excelId" class="easyui-validatebox" type="text">
				<div style="padding:20px">
					<table cellpadding="0" cellspacing="0" class="form-table">
						<tr>
							<td>起始行数：</td>
							<td><input id=excel_rowIndex name="rowIndex" class="easyui-validatebox" type="text" required="true"></input>
						</tr>
					</table>
				</div>
			</form>
			<div id="dlg-buttons" style="text-align:center">
				<a  href="#" class="easyui-linkbutton"  iconCls="icon-save"   onclick="javascript:updateItem()">保存</a>
				<a  href="#" class="easyui-linkbutton"  iconCls="icon-cancel" onclick="javascript:$('#update-excel-dialog').dialog('close')">关闭</a>
			</div>
		</div>
		<!-- 修改 Excel Dialog End-->
	
	</div>

</body>
